#ifndef CLIENTTOSERVICE_H
#define CLIENTTOSERVICE_H

#include <thread>
#include <mutex>
#include <semaphore.h>   //sem_t


using namespace std;


class ClientToService{
public:
    //autoReconnNum:  <0: reconnect all the time, >0: reconnet times of autoReconnNum
    //CLIENTTOSERVICE = new ClientToService(m_selfName, m_serviceIp, m_servicePort, -1, 50000);
    ClientToService(const string ID, const string serviceIp, const int servicePort, const int autoReconnNum, 
                    const int jsonAndimgBuffSize, const string taskDescribe="null",const int reconnectInterval=10
                    );
    ~ClientToService();
    int m_working(const int reconnectInterval=10);
    int m_sendStr2service(const string &strInfo);
    int m_sendBuff2Service(const char *buff, const int buffLen);

    bool m_getExit();
    void m_setExit(const bool val);

    bool m_getConnStatus();
    void m_setConnStatus(const bool val);

    string m_createJson_alarm(const int imgBuffSize, const float fx, const float fy);
    std::string m_createJson_deviceInfo(const float max, const float min, const float ave, const float diff, const int pos, const string time
                                                    , const int imgSize);
    int m_keepConnectThread(const int reconnectInterval=3); //all time to reconnect
    int m_communiteThread();

    int  m_connfd;

    char *mp_jsonAndImgBuff;
    int m_jsonAndImgBuffSize;

    char *mp_jsonAndImgBuff_deviceInfo; //don't use

    string m_selfName;

private:
    int mi_reconnectInterval;

    bool mb_autoReconnect;
    int m_reconnectNum;
    thread m_keepConnectThread_t;
    thread m_communiteThread_t;

    int m_connect();

    
    //reconnectInterval:   wait reconnectInterval second before reconnect
    


    string m_serviceIp;
    int m_servicePort;

    std::mutex m_mutexExit;
        bool m_exit;

    std::mutex m_mutexConnStatus;
        sem_t m_sem;
        bool m_connStatus;      //default: false
    string m_taskDescribe;

};



#endif